
package xin.cymall.controller;

import xin.cymall.annotation.SysLog;
import xin.cymall.entity.SysUser;
import xin.cymall.enumresource.DefaultEnum;
import xin.cymall.exception.ExcelException;
import xin.cymall.service.SysUserRoleService;
import xin.cymall.service.SysUserService;
import xin.cymall.utils.*;
import xin.cymall.validator.Assert;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.http.HttpServletRequest;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统用户
 * 
 * @author mhj
 * @email 254784163@qq.com
 * @date 2016年10月31日 上午10:40:10
 */
@Controller
@RequestMapping("/sys/user")
public class SysUserController extends AbstractController {
	@Autowired
	private CacheManager cacheManager;
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysUserRoleService sysUserRoleService;
	/**
	 * 所有用户列表
	 */

	@RequestMapping("/list")
	@RequiresPermissions("sys:user:list")
	public String list(){
		return "user/list";
	}

	/**
	 * 所有用户列表
	 */
	@ResponseBody
	@RequestMapping("/listData")
	@RequiresPermissions("sys:user:list")
	public R listData(@RequestParam Map<String, Object> params){
		//只有超级管理员，才能查看所有管理员列表
		if(getUserId() != Constant.SUPER_ADMIN){
			params.put("createUserId", getUserId());
		}

		//查询列表数据
		Query query = new Query(params);
		List<SysUser> userList = sysUserService.queryList(query);
		int total = sysUserService.queryTotal(query);

		PageUtils pageUtil = new PageUtils(userList, total, query.getLimit(), query.getPage());

		return R.ok().put("page", pageUtil);
	}
	/**
	 * 跳转到添加页面
	 */
	@RequestMapping("/add")
	public String add(){
		return "user/add";
	}
	/**
	 * @author mhj
	 * @Description  跳转到修改页面
	 * @param
	 * @date 2017/6/27 11:17
	 **/
	@RequestMapping("/edit/{id}")
	@RequiresPermissions("sys:user:list")
	public String edit(HttpServletRequest request, Model model, @PathVariable("id") Long id){
		SysUser user = sysUserService.queryObject(id);
		model.addAttribute("model",user);
		//获取所属角色
		List<Long> roleIds=sysUserRoleService.queryRoleIdList(user.getUserId());
		//将list转为字符串
		String roleIdList=StringUtils.join(roleIds.toArray(),",");
		model.addAttribute("roleIdList",roleIdList);
		return "/user/edit";
	}
	/**
	 * 获取登录的用户信息
	 */
	@ResponseBody
	@RequestMapping("/info")
	public R info(){
		return R.ok().put("user", getUser());
	}
	
	/**
	 * 修改登录用户密码
	 */
	@ResponseBody
	@SysLog("修改密码")
	@RequestMapping("/password")
	public R password(String password, String newPassword){
		if(getUserId()==1){
			throw new RRException("系统管理员不可修改密码");
		}
		Assert.isBlank(newPassword, "新密码不为能空");
		
		//sha256加密
		password = new Sha256Hash(password).toHex();
		//sha256加密
		newPassword = new Sha256Hash(newPassword).toHex();
				
		//更新密码
		int count = sysUserService.updatePassword(getUserId(), password, newPassword);
		if(count == 0){
			return R.error("原密码不正确");
		}
		
		//退出
		ShiroUtils.logout();
		
		return R.ok();
	}
	
	/**
	 * 用户信息
	 */
	@ResponseBody
	@RequestMapping("/info/{userId}")
	@RequiresPermissions("sys:user:list")
	public R info(@PathVariable("userId") Long userId){
		SysUser user = sysUserService.queryObject(userId);
		
		//获取用户所属的角色列表
		List<Long> roleIdList = sysUserRoleService.queryRoleIdList(userId);
		user.setRoleIdList(roleIdList);
		
		return R.ok().put("user", user);
	}
	
	/**
	 * 保存用户
	 */
	@ResponseBody
	@SysLog("保存用户")
	@RequestMapping("/save")
	@RequiresPermissions("sys:user:list")
	public R save(@RequestBody SysUser user){
		SysUser existUser = sysUserService.queryByUserName(user.getUsername());
		if(existUser!=null){
			return R.error("用户名已存在!");
		}
		user.setPassword(DefaultEnum.PASSWORD.getCode());
		user.setCreateUserId(getUserId());
		sysUserService.save(user);

		return R.ok();
	}
	
	/**
	 * 修改用户
	 */
	@ResponseBody
	@SysLog("修改用户")
	@RequestMapping("/update")
	@RequiresPermissions("sys:user:list")
	public R update(@RequestBody SysUser user){
		user.setCreateUserId(getUserId());
		sysUserService.update(user);
		
		return R.ok();
	}
	/**
	 * 解锁
	 */
	@ResponseBody
	@SysLog("解锁")
	@RequestMapping("/unlocked")
	@RequiresPermissions("sys:user:edit")
	public R unlocked(@RequestBody String[] usernames){
		Cache cache = cacheManager.getCache("passwordRetryCache");
		cache.clear();
		return R.ok();
	}
	
	/**
	 * 删除用户
	 */
	@ResponseBody
	@SysLog("删除用户")
	@RequestMapping("/delete")
	@RequiresPermissions("sys:user:list")
	public R delete(@RequestBody Long[] userIds){
		if(ArrayUtils.contains(userIds, 1L)){
			return R.error("系统管理员不能删除");
		}
		
		if(ArrayUtils.contains(userIds, getUserId())){
			return R.error("当前用户不能删除");
		}
		
		sysUserService.deleteBatch(userIds);
		
		return R.ok();
	}

	/**
	 * 初始化密码
	 */
	@ResponseBody
	@SysLog("初始化密码")
	@RequestMapping("/initPassword")
	@RequiresPermissions("sys:user:list")
	public R initPassword(@RequestBody Long[] userIds){
		if(ArrayUtils.contains(userIds, 1L)){
			return R.error("系统管理员不能初始化密码");
		}
		sysUserService.initPassword(userIds);

		return R.ok();
	}
	@ResponseBody
	@SysLog("生成excel")
	@RequestMapping("/genExcel")
	@RequiresPermissions("sys:user:list")
	public R genExcel(@RequestParam Map<String, Object> params,HttpServletRequest request) throws IOException, ExcelException{

		//查询列表数据
		params.put("offset", null);
		params.put("page", null);
		JSONArray array = JSONArray.fromObject(params.get("exportHead"));
		LinkedHashMap<String,Object> fieldMap = new LinkedHashMap<String,Object>();
		for(int i = 0; i < array.size();i++) {
			JSONObject jsonObj = (JSONObject)array.get(i);
			Iterator<?> it = jsonObj.keys();
			while(it.hasNext()) {
				String keys = it.next().toString();
				String values = jsonObj.getString(keys);
				fieldMap.put(keys,values);
			}
		}
		
		System.out.println(array);
		Query query = new Query(params);
		List<SysUser> userList = sysUserService.queryList(query);
		
		String savePath = request.getServletContext().getRealPath("/WEB-INF/tmp");
		logger.debug("获取到的/WEB-INF/tmp  路径=== " + savePath);
        
        File tmpDir = new File(savePath);
        // 判断上传文件的保存目录是否存在
        if (!tmpDir.exists() && !tmpDir.isDirectory()){
            logger.debug(savePath + "目录不存在，需要创建。");
            // 创建目录
            tmpDir.mkdir();
        }
        String fileWriteTime = DateUtil.getYmdhmsFName();
        
        String tmpFileName = savePath + "\\" + fileWriteTime + ".xls";
        logger.debug("临时文件名：===" + tmpFileName);
        File file = new File(tmpFileName);
        // 判断上传文件的保存目录是否存在
        if (!file.exists()){
            file.createNewFile();
            logger.debug("临时文件创建不成功。");
        }
        OutputStream out = new FileOutputStream(tmpFileName);
        ExcelUtils.listToExcel(userList, fieldMap,"sheet",0, out);
		return R.ok().put("filePath", tmpFileName);
	}
}
